//app.js

App({
  onShow:function(e){
    console.log(e)
    var that = this;
    var id = e.query.id;
    var shareid = e.query.shareid;
    var cardtype = e.query.cardtype;
    if (id && shareid && that.siteinfo.islogin) {
      // if (cardtype == 'common') {
      //   wx.redirectTo({
      //     url: '/pages/cardDetails/cardDetails?id=' + id,
      //   })
      // } else if (cardtype == 'comcard') {
      //   wx.redirectTo({
      //     url: '/pages/cardDetails/cardDetails?id=' + id,
      //   })
      // }
    }
  },
  onLaunch: function (e) {
    var that = this;
    wx.showLoading({
      title: '数据加载中',
    })
    var scene = e.scene;
    
    var id = e.query.id;
    var shareid = e.query.shareid;
    var cardtype = e.query.cardtype;

    wx.setStorageSync('id', id)
    wx.setStorageSync('scene', scene)
    wx.setStorageSync('shareid', shareid)
    wx.setStorageSync('cardtype', cardtype)

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    
    
    var requestpath = that.siteinfo.rootpath;
    // 登录
    wx.login({
      success: res => {
        if (res.code) {
          var code = res.code
          wx.getUserInfo({
            lang: 'zh_CN',
            success: function (res) {
              var rawData = res.rawData
              wx.request({
                url: requestpath + 'login', 
                data: { rawData: rawData, code: code ,scene: scene},
                method: 'get',
                success: function (res) {
                  if(res.data.code == 200){
                    that.siteinfo.islogin = true
                    that.siteinfo.userinfo = res.data.userinfo
                    wx.setStorageSync('userinfo', res.data.userinfo)
                    wx.setStorageSync('sessionkey', res.data.userinfo.sessionkey)
                    // if(id && shareid){
                    //   if(cardtype == 'common'){
                    //     wx.redirectTo({
                    //       url: '/pages/cardDetails/cardDetails?id='+id,
                    //     })
                    //   }else if(cardtype == 'comcard'){
                    //     wx.redirectTo({
                    //       url: '/pages/cardDetails/cardDetails?id=' + id,
                    //     })
                    //   }
                    // }
                  }else{
                    console.error('登陆失败');
                  }
                  wx.hideLoading()
                }
              })
            },
            fail: function () {
              wx.hideLoading()
              that.globalData.getUserInfoFail = true
              var fail = that.globalData.getUserInfoFail
              if (fail == true) {
                console.log('error');
                // wx.redirectTo({
                //   url: '/pages/welcome/welcome?scene='+scene
                // })
              }

            }
          })
        }
      }
    })
    // // 获取用户信息
    // wx.getSetting({
    //   success: res => {
    //     if (res.authSetting['scope.userInfo']) {
    //       // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
    //       wx.getUserInfo({
    //         success: res => {
    //           // 可以将 res 发送给后台解码出 unionId
    //           this.globalData.userInfo = res.userInfo
    //           console.log('onlaunch')
    //           // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //           // 所以此处加入 callback 以防止这种情况
    //           if (this.userInfoReadyCallback) {
    //             this.userInfoReadyCallback(res)
    //           }
    //         }
    //       })
    //     }
    //   }
    // })
  }, 
  onGotUserInfo: function (e) {
    var that = this;
    var scene = wx.getStorageSync('scene');
    wx.login({
      success: res => {
        if (res.code) {
          var code = res.code
          wx.showLoading({
            title: '',
          })
          wx.getUserInfo({
            lang: 'zh_CN',
            success: function (res) {

              var rawData = res.rawData
              wx.request({
                url: that.siteinfo.rootpath + 'login',
                data: { rawData: rawData, code: code, scene: scene },
                method: 'get',
                success: function (res) {
                  wx.hideLoading()
                  if (res.data.code == 200) {
                    that.siteinfo.islogin = true
                    that.siteinfo.userinfo = res.data.userinfo
                    wx.setStorageSync('userinfo', res.data.userinfo)
                    wx.setStorageSync('sessionkey', res.data.userinfo.sessionkey)
                  } else {
                    console.error('登陆失败');
                  }
                }
              })
            },
            fail: function () {
              // wx.redirectTo({
              //   url: '/pages/welcome/welcome?scene=' + scene
              // })
            }
          })
        }
      }
    })

  },
  globalData: {
    userInfo: null
  },
  siteinfo:require('siteinfo.js')
})